package allwe.controller;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class FileUploadController {
    //文件存储位置 指向文件服务器
    private final String FILE_SERVER = "http://127.0.0.1:8081/upload/";

    @ResponseBody
    @PostMapping("fileUpload1.do")
    public Map<String , String> upload1(MultipartFile headPhoto, HttpServletRequest req){
        Map<String,String> map = new HashMap<>();
        //控制文件大小
        if (headPhoto.getSize() > 1024 * 1024 * 5){
            map.put("message","文件不能大于5MB");
            return map;
        }


        //指定文件存储路径为项目部署环境下的upload目录
        String realPath = req.getServletContext().getRealPath("upload");
        //指定文件存储目录
        File dir = new File(realPath);
        //判断是否有这个目录
        if (dir.exists()){
            dir.mkdirs(); //可能放多层路径
        }

        //获取文件名称
        String filename = headPhoto.getOriginalFilename();
        //截取扩展名
        String extendsName = filename.substring(filename.lastIndexOf("."));
        //使用UUID替换文件名
        String fileId = UUID.randomUUID().toString();
        //新的文件名
        String newFileName = fileId.concat(extendsName);

        //指向本地地址  文件存储位置
        File file = new File(dir, newFileName);
        try {
            //文件写入
            headPhoto.transferTo(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        map.put("message","OK");
        map.put("newFileName",newFileName);
        map.put("fileType", headPhoto.getContentType());
        return map;
    }

    @ResponseBody
    @PostMapping("fileUpload.do")
    public Map<String , String> upload(MultipartFile headPhoto){
        Map<String,String> map = new HashMap<>();

        //获取文件名称
        String filename = headPhoto.getOriginalFilename();
        //截取扩展名
        String extendsName = filename.substring(filename.lastIndexOf("."));
        //使用UUID替换文件名
        String fileId = UUID.randomUUID().toString();
        //新的文件名
        String newFileName = fileId.concat(extendsName);

       try {
           // 创建 jersey 包中的 client 对象
           Client client = Client.create();
           //打开目标资源
           WebResource resource = client.resource(FILE_SERVER + newFileName);
           // 保存文件
            resource.put(String.class,headPhoto.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        map.put("message","OK");
        map.put("newFileName",newFileName);
        map.put("fileType", headPhoto.getContentType());
        map.put("fileServer",FILE_SERVER);
        return map;
    }


}
